package com.jiaai.cloud.wx.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.entity.RestResultGenerator;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.redis.util.RedisUtil;
import com.jiaai.cloud.base.util.InviteCode;
import com.jiaai.cloud.base.weixin.common.MD5;
import com.jiaai.cloud.wx.entity.TAdmin;
import com.jiaai.cloud.wx.entity.TPay;
import com.jiaai.cloud.wx.service.ITAdminService;
import com.jiaai.cloud.wx.service.ITEmpService;
import com.jiaai.cloud.wx.service.ITPayService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-28
 */
@Controller
@RequestMapping("/api/v1/tadmin")
public class TAdminController {
    @Autowired
    private ITAdminService itAdminService;
    @Autowired
    private ITPayService itPayService;
    @Autowired
    private ITEmpService itEmpService;
    @Autowired
    private RedisUtil redisUtil;

    //todo tested
    /**
     * 注册管理员
     * @param tAdmin
     * @return  带有管理员id的tAdmin
     * @throws ServiceException
     */
    @RequestMapping(value = "/reg",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object reg(@RequestBody TAdmin tAdmin)  throws ServiceException {
        try {
            tAdmin.setAdminid("" + new Date().getTime());
            MD5.MD5Encode(tAdmin.getAdminpassword());
            itAdminService.insert(tAdmin);
            return RestResultGenerator.genSuccessResult(tAdmin);
        }catch (Exception e) {
            throw new ServiceException("注册失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 查询员工, 并分页
     * @param page
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/queryemppage/{groupid}",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object queryEmpPage(FrontPage page, @PathVariable("groupid") Integer groupid)  throws ServiceException {
        try {
            return itEmpService.selectGroupEmp(page.getPagePlus(), groupid);
        }catch (Exception e) {
            throw new ServiceException("查询失败", e.getMessage());
        }
    }

//    //
//    /**
//     * 管理员批量发放工资, 工资信息转为正在转入的状态, 由任务调度类来对接支付宝api
//     * @param ids
//     * @return
//     * @throws ServiceException
//     */
//    @RequestMapping(value = "/paybatch",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
//    @ResponseBody
//    public Object paybatch(int[] ids)  throws ServiceException {
//        try {
//            itAdminService.payBatch(ids);
//            return RestResultGenerator.genSuccessResult("批量发放成功");
//        }catch (Exception e) {
//            throw new ServiceException("批量发放失败", e.getMessage());
//        }
//    }

    //todo
    /**
     * 为员工添加工资信息
     * @param tPay
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/createpay",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object createPay(@RequestBody TPay tPay)  throws ServiceException {
        tPay.setCreatedate(new Date());
        tPay.setStatus(0);
        tPay.setDeleted(0);
        try {
            itPayService.insert(tPay);
            return RestResultGenerator.genSuccessResult("添加工资信息成功");
        }catch (Exception e) {
            throw new ServiceException("添加工资信息失败", e.getMessage());
        }
    }

    //todo tested
    /**
     *  管理员生成邀请码, 5分钟有效期, 邀请码含有被邀请人的的分组groupid
     * @param groupid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/createinvitecode",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object createInviteCode(Integer groupid)  throws ServiceException {
        try {
            String inviteCode = InviteCode.create().concat("@")+groupid;
            redisUtil.set(1, inviteCode, inviteCode, 300);

            //EmailUtil.sendEmail(email);

            return RestResultGenerator.genSuccessResult(inviteCode);
        }catch (Exception e) {
            throw new ServiceException("邀请码生成失败", e.getMessage());
        }
    }

    //todo tested
    /**
     * 删除分组, 并且逻辑删除组内的所有员工以及员工的工资
     * @param id
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/deletegroupemp/{id}",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object deleteGroupEmp(@PathVariable("id") Integer id)  throws ServiceException {
        try {
            itAdminService.deleteGroupEmp(id);
            return RestResultGenerator.genSuccessResult("删除分组和员工成功");
        }catch (Exception e) {
            throw new ServiceException("删除分组和员工失败", e.getMessage());
        }
    }

    //todo
    /**
     * 员工的批量逻辑删除
     * @param empids
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/deletebatch",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object deleteBatch(@RequestParam("empids")  String empids) throws ServiceException {
        if(StringUtils.isEmpty(empids)){
            throw new ServiceException("参数异常");
        }
        try {
            if(empids.indexOf(",")<0){
                itEmpService.deleteById(empids);
            }else{
                itEmpService.deleteBatchIds(Arrays.asList(empids.split(",")));
            }
            return  RestResultGenerator.genSuccessResult();
        }catch (Exception e) {
            throw new ServiceException("批量删除员工失败", e.getMessage());
        }
    }

    //todo
    /**
     * 员工的批量迁移分组
     * @param empids
     * @param groupid
     * @return
     * @throws ServiceException
     */
    @RequestMapping(value = "/changegroup",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    public Object changeGroupBatch(@RequestParam("empids") String empids,@RequestParam("groupid") Integer groupid) throws ServiceException {
        String[] empidds;
        if(StringUtils.isEmpty(empids)){
            throw new ServiceException("参数异常");
        }
        try {
            if(empids.indexOf(",") < 0){
                empidds = new String[]{empids};
            }else{
                empidds= empids.split(",");
            }
            itEmpService.changeGroupBatch(empidds, groupid);
            return  RestResultGenerator.genSuccessResult("批量迁移成功");
        }catch (Exception e) {
            throw new ServiceException("批量迁移失败", e.getMessage());
        }

    }

    //todo
    @RequestMapping(value = "/fortest",method = RequestMethod.POST,produces = {"application/json;charset=UTF-8"} )
    @ResponseBody
    @Transactional
    public Object fortest(String email)  throws ServiceException {
        TAdmin tAdmin = new TAdmin();
        tAdmin.setAdminid(""+new Date().getTime());
        try {
            itAdminService.insert(tAdmin);
            itAdminService.insert(tAdmin);
            return RestResultGenerator.genSuccessResult("");
        }catch (Exception e) {
            throw new ServiceException("邀请码生成失败", e.getMessage());
        }
    }

}
